package com.achievo.know.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import com.dayatang.domain.BaseEntity;
import com.dayatang.domain.InstanceFactory;

@Entity
@Table(name="RAMS_CATEGORY")
public class Category extends BaseEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 9083470097080790262L;

	//名称
	@NotNull
	@Column(name="NAME",length=50,nullable=false)
	private String name;
	
	//位置
	@NotNull
	@Column(name="POSITION",nullable=false)
	private int position;
	
	//是否显示在导航
	@NotNull
	@Column(name="SHOW_IN_NAV",nullable=false)
	private boolean showInNav;
	
	@Transient
	private String showStr;
	
	//父实体
	@ManyToOne
	@JoinColumn(name="FK_PARENT_ID")
	private Category parent;
	
	//子实体
	@OneToMany(fetch=FetchType.LAZY)
	@JoinColumn(name="FK_PARENT_ID")
	@OrderBy(value="position")
	private List<Category> children;

	@Override
	public boolean equals(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}
	

	public String getShowStr() {
		return showStr;
	}

	public void setShowStr(String showStr) {
		this.showStr = showStr;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public boolean isShowInNav() {
		return showInNav;
	}

	public void setShowInNav(boolean showInNav) {
		this.showInNav = showInNav;
	}

	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	public List<Category> getChildren() {
		return children;
	}

	public void setChildren(List<Category> children) {
		this.children = children;
	}
	
	private static CategoryRepository categoryRepository;

	public static CategoryRepository getCategoryRepository() {
		if (categoryRepository == null) {
			categoryRepository = InstanceFactory.getInstance(CategoryRepository.class);
		}
		return categoryRepository;
	}
	
	public void add(){
		getCategoryRepository().save(this);
	}
	
	
	
}
